﻿using PYNET.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace PYNET.Utils
{
    public class ImageHelper
    {
        /// <summary>
        /// 图片按照实际比例放大
        /// </summary>
        /// <param name="content"></param>
        /// <param name="tailorInfo"></param>
        /// <returns></returns>
        public static string TailorImage(Stream content, TailorInfo tailorInfo ,String FileName)
        {
            var scaleWidth = Convert.ToInt16(tailorInfo.PictureWidth);
            var scaleHeight = Convert.ToInt16(tailorInfo.PictureHeight);

            Bitmap sourceBitmap = new Bitmap(content);
            double scaleWidthPercent = Convert.ToDouble(sourceBitmap.Width) / scaleWidth;
            double scaleHeightPercent = Convert.ToDouble(sourceBitmap.Height) / scaleHeight;

            double realX = scaleWidthPercent * tailorInfo.CoordinateX;
            double realY = scaleHeightPercent * tailorInfo.CoordinateY;
            double realWidth = scaleWidthPercent * tailorInfo.CoordinateWidth;
            double realHeight = scaleHeightPercent * tailorInfo.CoordinateHeight;

            return CropImage(content, (int)realX, (int)realY, (int)realWidth, (int)realHeight,FileName);
        }

        /// <summary>
        /// 生成新图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="cropWidth"></param>
        /// <param name="cropHeight"></param>
        /// <returns></returns>
        public static string CropImage(byte[] content, int x, int y, int cropWidth, int cropHeight, String FileName)
        {
            using (MemoryStream stream = new MemoryStream(content))
            {
                return CropImage(stream, x, y, cropWidth, cropHeight,FileName);
            }
        }

        /// <summary>
        /// 生成新图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="cropWidth"></param>
        /// <param name="cropHeight"></param>
        /// <returns></returns>
        public static string CropImage(Stream content, int x, int y, int cropWidth, int cropHeight, String FileName)
        {
            using (Bitmap sourceBitmap = new Bitmap(content))
            {
                Bitmap bitSource = new Bitmap(sourceBitmap, sourceBitmap.Width, sourceBitmap.Height);
                Rectangle cropRect = new Rectangle(x, y, cropWidth, cropHeight);
                using (Bitmap newBitMap = new Bitmap(cropWidth, cropHeight))
                {
                    newBitMap.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
                    using (Graphics g = Graphics.FromImage(newBitMap))
                    {
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.DrawImage(bitSource, new Rectangle(0, 0, newBitMap.Width, newBitMap.Height), cropRect, GraphicsUnit.Pixel);
                        return BitmapToBytes(newBitMap,FileName);
                    }
                }
            }
        }

        /// <summary>
        /// 图片转到byte数组
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string BitmapToBytes(Bitmap source, String FileName)
        {
            ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[4];
            EncoderParameters parameters = new EncoderParameters(1);
            parameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
            using (MemoryStream tmpStream = new MemoryStream())
            {
                source.Save(tmpStream, codec, parameters);
                byte[] data = new byte[tmpStream.Length];
                tmpStream.Seek(0, SeekOrigin.Begin);
                tmpStream.Read(data, 0, (int)tmpStream.Length);
                return SaveImageToLocal(data,FileName);
            }
        }

        /// <summary>
        /// 存储图片到本地文件系统
        /// </summary>
        /// <param name="binary"></param>
        public static string SaveImageToLocal(byte[] binary, String FileName)
        {
            //根据用户信息生成图片名 todo

            //var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Content\avatar.png");
            var FilePath = Path.Combine("StaticFiles","UserPic", FileName);
            File.WriteAllBytes(FilePath, binary);
            return "data:images/png;base64," + Convert.ToBase64String(binary);//Data URI Scheme形式
            //return @"/Content/avatar.png";//Url形式
        }
    }
}
